//package com.ia.aistream.manager.framework.config;
//
//import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
//import com.ia.aistream.common.utils.StringUtils;
//import com.ia.aistream.common.utils.spring.SpringUtils;
//import com.ia.aistream.manager.framework.config.properties.ShiroConf;
//import com.ia.aistream.manager.framework.shiro.realm.UserRealm;
//import com.ia.aistream.manager.framework.shiro.session.OnlineSessionDAO;
//import com.ia.aistream.manager.framework.shiro.session.OnlineSessionFactory;
//import com.ia.aistream.manager.framework.shiro.web.filter.LogoutFilter;
//import com.ia.aistream.manager.framework.shiro.web.filter.captcha.CaptchaValidateFilter;
//import com.ia.aistream.manager.framework.shiro.web.filter.kickout.KickoutSessionFilter;
//import com.ia.aistream.manager.framework.shiro.web.filter.online.OnlineSessionFilter;
//import com.ia.aistream.manager.framework.shiro.web.filter.sync.SyncOnlineSessionFilter;
//import com.ia.aistream.manager.framework.shiro.web.session.OnlineWebSessionManager;
//import com.ia.aistream.manager.framework.shiro.web.session.SpringSessionValidationScheduler;
//import org.apache.commons.io.IOUtils;
//import org.apache.shiro.cache.ehcache.EhCacheManager;
//import org.apache.shiro.codec.Base64;
//import org.apache.shiro.config.ConfigurationException;
//import org.apache.shiro.io.ResourceUtils;
//import org.apache.shiro.mgt.SecurityManager;
//import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
//import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
//import org.apache.shiro.web.mgt.CookieRememberMeManager;
//import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
//import org.apache.shiro.web.servlet.SimpleCookie;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import javax.servlet.Filter;
//import java.io.ByteArrayInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.LinkedHashMap;
//import java.util.Map;
//
///**
// * 权限配置加载
// *
// * @author ia
// */
//@Configuration
//public class ShiroConfig
//{
//    public static final String PREMISSION_STRING = "perms[\"{0}\"]";
//
//    //拦截地址
//    @Autowired(required = false)
//    private ShiroConf shiroConf;
//
//    /**
//     * 缓存管理器 使用Ehcache实现
//     */
//    @Bean
//    public EhCacheManager getEhCacheManager()
//    {
//        net.sf.ehcache.CacheManager cacheManager = net.sf.ehcache.CacheManager.getCacheManager("ruoyi");
//        EhCacheManager em = new EhCacheManager();
//        if (StringUtils.isNull(cacheManager))
//        {
//            em.setCacheManager(new net.sf.ehcache.CacheManager(getCacheManagerConfigFileInputStream()));
//            return em;
//        }
//        else
//        {
//            em.setCacheManager(cacheManager);
//            return em;
//        }
//    }
//
//    /**
//     * 返回配置文件流 避免ehcache配置文件一直被占用，无法完全销毁项目重新部署
//     */
//    protected InputStream getCacheManagerConfigFileInputStream()
//    {
//        String configFile = "classpath:ehcache/ehcache-shiro.xml";
//        InputStream inputStream = null;
//        try
//        {
//            inputStream = ResourceUtils.getInputStreamForPath(configFile);
//            byte[] b = IOUtils.toByteArray(inputStream);
//            InputStream in = new ByteArrayInputStream(b);
//            return in;
//        }
//        catch (IOException e)
//        {
//            throw new ConfigurationException(
//                    "Unable to obtain input stream for cacheManagerConfigFile [" + configFile + "]", e);
//        }
//        finally
//        {
//            IOUtils.closeQuietly(inputStream);
//        }
//    }
//
//    /**
//     * 自定义Realm
//     */
//    @Bean
//    public UserRealm userRealm(EhCacheManager cacheManager)
//    {
//        UserRealm userRealm = new UserRealm();
//        userRealm.setCacheManager(cacheManager);
//        return userRealm;
//    }
//
//    /**
//     * 自定义sessionDAO会话
//     */
//    @Bean
//    public OnlineSessionDAO sessionDAO()
//    {
//        OnlineSessionDAO sessionDAO = new OnlineSessionDAO();
//        return sessionDAO;
//    }
//
//    /**
//     * 自定义sessionFactory会话
//     */
//    @Bean
//    public OnlineSessionFactory sessionFactory()
//    {
//        OnlineSessionFactory sessionFactory = new OnlineSessionFactory();
//        return sessionFactory;
//    }
//
//    /**
//     * 会话管理器
//     */
//    @Bean
//    public OnlineWebSessionManager sessionManager()
//    {
//        OnlineWebSessionManager manager = new OnlineWebSessionManager();
//        // 加入缓存管理器
//        manager.setCacheManager(getEhCacheManager());
//        // 删除过期的session
//        manager.setDeleteInvalidSessions(true);
//        // 设置全局session超时时间
//        manager.setGlobalSessionTimeout(shiroConf.getSession().getExpireTime() * 60 * 1000);
//        // 去掉 JSESSIONID
//        manager.setSessionIdUrlRewritingEnabled(false);
//        // 定义要使用的无效的Session定时调度器
//        manager.setSessionValidationScheduler(SpringUtils.getBean(SpringSessionValidationScheduler.class));
//        // 是否定时检查session
//        manager.setSessionValidationSchedulerEnabled(true);
//        // 自定义SessionDao
//        manager.setSessionDAO(sessionDAO());
//        // 自定义sessionFactory
//        manager.setSessionFactory(sessionFactory());
//        return manager;
//    }
//
//    /**
//     * 安全管理器
//     */
//    @Bean
//    public SecurityManager securityManager(UserRealm userRealm)
//    {
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        // 设置realm.
//        securityManager.setRealm(userRealm);
//        // 记住我
//        securityManager.setRememberMeManager(rememberMeManager());
//        // 注入缓存管理器;
//        securityManager.setCacheManager(getEhCacheManager());
//        // session管理器
//        securityManager.setSessionManager(sessionManager());
//        return securityManager;
//    }
//
//    /**
//     * 退出过滤器
//     */
//    public LogoutFilter logoutFilter()
//    {
//        LogoutFilter logoutFilter = new LogoutFilter();
//        logoutFilter.setCacheManager(getEhCacheManager());
//        logoutFilter.setLoginUrl(shiroConf.getUser().getLoginUrl());
//        return logoutFilter;
//    }
//
//    /**
//     * Shiro过滤器配置
//     */
//    @Bean
//    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager)
//    {
//        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//        // Shiro的核心安全接口,这个属性是必须的
//        shiroFilterFactoryBean.setSecurityManager(securityManager);
//        // 身份认证失败，则跳转到登录页面的配置
//        shiroFilterFactoryBean.setLoginUrl(shiroConf.getUser().getLoginUrl());
//        // 权限认证失败，则跳转到指定页面
//        shiroFilterFactoryBean.setUnauthorizedUrl(shiroConf.getUser().getUnauthorizedUrl());
//        // 系统权限列表
//        // filterChainDefinitionMap.putAll(SpringUtils.getBean(IMenuService.class).selectPermsAll());*/
//        Map<String, Filter> filters = new LinkedHashMap<String, Filter>();
//        filters.put("onlineSession", onlineSessionFilter());
//        filters.put("syncOnlineSession", syncOnlineSessionFilter());
//        filters.put("captchaValidate", captchaValidateFilter());
//        filters.put("kickout", kickoutSessionFilter());
//        // 注销成功，则跳转到指定页面
//        filters.put("logout", logoutFilter());
//        shiroFilterFactoryBean.setFilters(filters);
//        shiroFilterFactoryBean.setFilterChainDefinitionMap(shiroConf.getFilterChainDefinitionMap());
//
//        return shiroFilterFactoryBean;
//    }
//
//    /**
//     * 自定义在线用户处理过滤器
//     */
//    @Bean
//    public OnlineSessionFilter onlineSessionFilter()
//    {
//        OnlineSessionFilter onlineSessionFilter = new OnlineSessionFilter();
//        onlineSessionFilter.setLoginUrl(shiroConf.getUser().getLoginUrl());
//        return onlineSessionFilter;
//    }
//
//    /**
//     * 自定义在线用户同步过滤器
//     */
//    @Bean
//    public SyncOnlineSessionFilter syncOnlineSessionFilter()
//    {
//        SyncOnlineSessionFilter syncOnlineSessionFilter = new SyncOnlineSessionFilter();
//        return syncOnlineSessionFilter;
//    }
//
//    /**
//     * 自定义验证码过滤器
//     */
//    @Bean
//    public CaptchaValidateFilter captchaValidateFilter()
//    {
//        CaptchaValidateFilter captchaValidateFilter = new CaptchaValidateFilter();
//        captchaValidateFilter.setCaptchaEnabled(shiroConf.getUser().isCaptchaEnabled());
//        captchaValidateFilter.setCaptchaType(shiroConf.getUser().getCaptchaType());
//        return captchaValidateFilter;
//    }
//
//    /**
//     * cookie 属性设置
//     */
//    public SimpleCookie rememberMeCookie()
//    {
//        SimpleCookie cookie = new SimpleCookie("rememberMe");
//        cookie.setDomain(shiroConf.getCookie().getDomain());
//        cookie.setPath(shiroConf.getCookie().getPath());
//        cookie.setHttpOnly(shiroConf.getCookie().isHttpOnly());
//        cookie.setMaxAge(shiroConf.getCookie().getMaxAge() * 24 * 60 * 60);
//        return cookie;
//    }
//
//    /**
//     * 记住我
//     */
//    public CookieRememberMeManager rememberMeManager()
//    {
//        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
//        cookieRememberMeManager.setCookie(rememberMeCookie());
//        cookieRememberMeManager.setCipherKey(Base64.decode("fCq+/xW488hMTCD+cmJ3aQ=="));
//        return cookieRememberMeManager;
//    }
//
//    /**
//     * 同一个用户多设备登录限制
//     */
//    public KickoutSessionFilter kickoutSessionFilter()
//    {
//        KickoutSessionFilter kickoutSessionFilter = new KickoutSessionFilter();
//        kickoutSessionFilter.setCacheManager(getEhCacheManager());
//        kickoutSessionFilter.setSessionManager(sessionManager());
//        // 同一个用户最大的会话数，默认-1无限制；比如2的意思是同一个用户允许最多同时两个人登录
//        kickoutSessionFilter.setMaxSession(shiroConf.getSession().getMaxSession());
//        // 是否踢出后来登录的，默认是false；即后者登录的用户踢出前者登录的用户；踢出顺序
//        kickoutSessionFilter.setKickoutAfter(shiroConf.getSession().isKickoutAfter());
//        // 被踢出后重定向到的地址；
//        kickoutSessionFilter.setKickoutUrl("/login?kickout=1");
//        return kickoutSessionFilter;
//    }
//
//    /**
//     * thymeleaf模板引擎和shiro框架的整合
//     */
//    @Bean
//    public ShiroDialect shiroDialect()
//    {
//        return new ShiroDialect();
//    }
//
//    /**
//     * 开启Shiro注解通知器
//     */
//    @Bean
//    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(
//            @Qualifier("securityManager") SecurityManager securityManager)
//    {
//        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
//        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
//        return authorizationAttributeSourceAdvisor;
//    }
//}
